if (!exists("app_root"))
    {
    stop("Need to have app_root defined to source this template");
    }

source( paste( app_root, "cgi-lib/R/ep.io.R", sep = "/" ) );

require( affy, keep.source = FALSE );
require( biomaRt, keep.source = FALSE );

biomart_affy_array_id_to_cdfname = function(biomart_affy_array_id)
    {
    cleancdfname( substring( biomart_affy_array_id, 6 ), FALSE );
    }

ep.readaffy = function( tempceldir, affydatafile, affycols, affyrows, species )
    {
    biomart_datasets = list( agambiae = "agambiae_gene_ensembl"
                            , btaurus = "btaurus_gene_ensembl"
                            , celegans = "celegans_gene_ensembl"
                            , mmulatta = "mmulatta_gene_ensembl"
                            , cfamiliaris = "cfamiliaris_gene_ensembl"
                            , dmelanogaster = "dmelanogaster_gene_ensembl"
                            , drerio = "drerio_gene_ensembl"
                            , ggallus = "ggallus_gene_ensembl"
                            , hsapiens = "hsapiens_gene_ensembl"
                            , mmusculus = "mmusculus_gene_ensembl"
                            , rnorvegicus = "rnorvegicus_gene_ensembl"
                            , scerevisiae = "scerevisiae_gene_ensembl"
                            );

    affydata = ReadAffy( celfile.path = tempceldir );
    cdfInfo = getCdfInfo( affydata );
    save( affydata, file = affydatafile );

    columns = cbind( seq( 0, length(affydata) - 1 ), sampleNames(affydata) );
    write.table( columns, col.names = c( "ID", "column_id"), file = affycols, quote = FALSE, row.names = FALSE, sep = "\t" );

    dataset = biomart_datasets[[species]];
    has_affyrows_written = FALSE;

    if ( !is.null(dataset) )
        {
        mart = useMart( biomart = "ensembl", dataset = dataset );
#       mart = useMart( biomart = "ensembl", dataset = dataset, mysql = TRUE, user = "anonymous", password = "" );
        affy_arrays = getAffyArrays(mart);
        array = affy_arrays$name[lapply( affy_arrays$name, biomart_affy_array_id_to_cdfname ) == cleancdfname( affydata@cdfName, FALSE )];

        if ( length(array) > 0 )
            {
            g = getGene( geneNames( affydata ), array = array[[1]], mart = mart );

            if (exists("g"))
                {
                m = match( geneNames ( affydata ), g$ID );
                rows = cbind( seq( 0, length(geneNames(affydata)) - 1 ), geneNames(affydata), g$symbol[m], g$description[m], geneNames(affydata), paste( g$chromosome[m], g$band[m], sep = "" ), g$symbol[m] );
                write.table( rows, col.names = c( "ID", "affymetrix", "geneSymbol", "Description", "feature_id", "chromosome_location", "composite_feature_id" ), file = affyrows, quote = FALSE, row.names = FALSE, sep = "\t" );
                has_affyrows_written = TRUE;
                }
            }
        }

    if ( !has_affyrows_written )
        {
        rows = cbind( seq( 0, length(geneNames(affydata)) - 1 ), geneNames(affydata) );
        write.table( rows, col.names = c( "ID", "affymetrix" ), file = affyrows, quote = FALSE, row.names = FALSE, sep = "\t" );
        }
    }
